export function getBirthdayByIdCard(idCard) {
  if (idCard.length === 15) {
    return [
      "19" + idCard.substr(6, 2),
      idCard.substr(8, 2),
      idCard.substr(10, 2),
    ].join("-");
  } else if (idCard.length === 18) {
    return [
      idCard.substr(6, 4),
      idCard.substr(10, 2),
      idCard.substr(12, 2),
    ].join("-");
  }

  return "";
}

//0 = 女，1 = 男
export function getSexByIdCard(idCard) {
  if (idCard.length === 15) {
    return (idCard.substr(14, 1) % 2) + '';
  } else if (idCard.length === 18) {
    return (idCard.substr(16, 1) % 2) + '';
  }
  return "";
}

//获取年龄
export function getAgeByIdCard(idCard) {
  const sexAndAge = {};
  //获取用户身份证号码
  const userCard = idCard;

  //如果用户身份证号码为undefined则返回空
  if (!userCard) {
    return sexAndAge;
  }

  // 获取出生日期
  const yearBirth = userCard.substring(6, 10);
  const monthBirth = userCard.substring(10, 12);
  const dayBirth = userCard.substring(12, 14);
  // 获取当前年月日并计算年龄
  const myDate = new Date();
  const monthNow = myDate.getMonth() + 1;
  const dayNow = myDate.getDate();
  let age = myDate.getFullYear() - yearBirth;

  if (monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)) {
    age--;
  }

  // 得到年龄
  sexAndAge.age = age;

  return sexAndAge.age;
}

/**
 * 检查号码是否符合规范，包括长度，类型 
 * */
export function IdentityCode_isCardNo(card) {
  //身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X  
  var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/; 
  if (reg.test(card) === false) {
      return false;
  }
  return true;
}

